package com.lz.helper.face

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.io.File
import java.text.DecimalFormat
import kotlin.math.sqrt


//未检测出人脸
const val ERR_DETECT = 1

//未检测出人脸特征值
const val ERR_FEATURE = 2

//未检测出超时
const val ERR_TIMEOUT = 3

// 1:1
fun distance(p1: FloatArray, p2: FloatArray): Float {
    var rss = 0f
    var square = 0f
    var sq = 0f
    for (j in p2.indices) {
        sq += p1[j] * p1[j]
        square += p2[j] * p2[j]
        rss += p2[j] * p1[j]
    }
    return rss / (sqrt(square.toDouble()) * sqrt(sq.toDouble())).toFloat()
}


fun float2Double(float: Float): Float = DecimalFormat("###.00").format(float * 100).toFloat()

fun loadBitmap(file: File): Bitmap? {
    try {
        if (file.exists() && file.isFile) {
            return BitmapFactory.decodeFile(file.absolutePath)
        }
    } catch (_: Exception) {
    }
    return null
}

fun loadBitmap(path: String): Bitmap? {
    try {
        return BitmapFactory.decodeFile(path)
    } catch (_: Exception) {
    }
    return null
}
